package casino;

import java.io.*;
import java.net.*;
import javax.swing.*;

public class NetworkTest{
	/*
	protected static Socket client;
	protected static ServerSocket server;
	private static Thread tester;
	private static Thread checker;
	private static boolean connected = false;
	
	public static void checkHosts(String subnet){
		checker = new LanScanThread(subnet);
		checker.start();
	}
	
	public static void listen(){
		tester = new ServerThread();
		tester.start();
	}
	
	public static void requestRecieved(Socket i){
		if(i.equals(client)){
			return;
		}
		if(client == null){
			try{
				setClient(i);
				Thread.sleep(20);
				BufferedReader read = new BufferedReader(new InputStreamReader(client.getInputStream()));
				String s = read.readLine();
				if(s.equals("casino")){
					PrintWriter write = new PrintWriter(client.getOutputStream(), true);
					write.println("casino_confirmed");
					connected = true;
				}else{
					client.close();
					client = null;
				}
			}catch (Exception e){
				e.printStackTrace();
			}
		}
	}
	
	public static boolean testClient(InetAddress i){
		if(client == null){
			try{
				setClient(new Socket(i, 19302));
				out(client.isClosed());
				PrintWriter p = new PrintWriter(client.getOutputStream(), true);
				BufferedReader b = new BufferedReader(new InputStreamReader(client.getInputStream()));
				p.println("casino");
				Thread.sleep(100);
				String s = b.readLine();
				if(s.equals("casino_confirmed")){
					connected = true;
					return true;
				}
				client.close();
				client = null;
				return false;
			}catch (Exception e){
				return false;
			}
		}else{
			return false;
		}
	}
	
	public static synchronized void setClient(Socket s){
		client = s;
	}
	
	public static void main(String[] args){
		try {
			server = new ServerSocket(19302);
			out("Listening for Peer...");
			listen();
			checkHosts("192.168.1");
			while(!connected){
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			out("Peer Found");
			out("");
			PrintWriter toPeer = new PrintWriter(client.getOutputStream(), true);
			BufferedReader fromPeer = new BufferedReader(new InputStreamReader(client.getInputStream()));
			WriteThread writeThread = new WriteThread(toPeer);
			ReadThread readThread = new ReadThread(fromPeer);
			writeThread.start();
			readThread.start();
			while(writeThread.isAlive() && readThread.isAlive()){
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			toPeer.close();
			fromPeer.close();
			server.close();
			client.close();
		} catch (IOException e2) {
			e2.printStackTrace();
		}
	}
	
	public static void out(Object o){
		System.out.println(o.toString());
	}
	*/
}
